package com.mxf.code.repository;


import com.mxf.code.entity.node.Person;
import com.mxf.code.response.PersonFriendResponse;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.neo4j.repository.query.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

/**
 * @author 28382
 */
@Repository
public interface PersonRepository extends Neo4jRepository<Person, Long> {

//    @Query("MATCH (n:User) RETURN n ")

//    List<Person> getUserNodeList();

//

//    @Query("create (n:User{name:{name},age:{age},sex:{sex}}) RETURN n ")

//    Person addUserNode(@Param("name") String name, @Param("age")int age, @Param("sex") String sex);

    //@Query("MATCH (n) WHERE id(n) = :#{#userNode.nodeId} SET n.name = :#{#userNode.name},n.age = :#{#userNode.age},n.sex = :#{#userNode.sex} RETURN n")
    //Person updateByNode(@Param("userNode") Person userNode);

    @Query("match (n:Person {name:{0}}),(m:Person {name:{2}})" + "create (n)-[:职场{relation:{1}}]->(m)")
    void createRelation(String from, String relation, String to);

    @Query("MATCH (n:Person {name:{name}}) RETURN n limit 1")
    Person selectByName(@Param("name") String name);

    @Query("match (n:Person {name:{0}}),(m:Person {name:{1}})" + "create (n)-[:FRIENDS]->(m)")
    void createRelation(String from, String to);

    @Query("match (person)-[:FRIENDS]->(friend) return id(person) As id, person.name as personName,friend.name as friendName")
    List<PersonFriendResponse> allPersonFriends();

    @Query("CREATE (p:Person $props) RETURN p")
    Person savePerson(@Param("props") Map<String, Object> props);

    @Query("MATCH (p:Person {name: $name}) SET p += $props " +
            "RETURN p")
    Person updatePropertiesByName(@Param("name") String name, @Param("props") Map<String, Object> props);

    @Query("MATCH (t:本体 {name:{type}}) CREATE (p:Person $props)-[:属于]->(t) RETURN p")
    Person savePerson(@Param("type") String type, @Param("props") Map<String, Object> props);

    @Query("CREATE (t:本体 {name:{0}})")
    void saveOntology(@Param("type") String type);
}

